<?php


namespace App\Models\CacheModel;


use App\Models\Api\BaseCache;
use think\Exception;
use AstroTool\Cache\CacheManage;

/**
 * app更新模型
 * Class AppVersion
 * @package App\Models\CacheModel
 */
class AppVersion extends BaseCache
{
    protected $table = 'wechatadmin.system_app_version';

    protected $cacheModel = 'config.version';

    /**
     * 版本更新
     * @param array $map
     * @return array|bool|mixed
     * @throws \Exception
     */
    public function getVersionData($map = [])
    {
        // 获取最新的版本信息
        $newData = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $map['type'])->getDataAndSet(function () use ($map)
        {
            return $this->getVersionDataByDb($map['type']);
        });
        // 判断当前请求头 version是否存在并且不是最新的版本
        if ($newData)
        {
            if (isset($map['version']) && $map['version'] && $newData['version_name'] !== 'V' . $map['version'])
            {
                $isAstroDataFromLocal = $this->getVersionInfo($map['type'], $map['version'], ['is_astro_data_from_local', 'android_audit_mode_swtich', 'goods_pay_type', 'coin_pay_type', 'is_apple_pay', 'apple_goods', 'logo_type', 'is_open_ad']);
                if (is_array($isAstroDataFromLocal))
                {
                    //获取当前版本星盘本地化状态
                    if (isset($isAstroDataFromLocal['is_astro_data_from_local']))
                    {
                        $newData['is_astro_data_from_local'] = (int)$isAstroDataFromLocal['is_astro_data_from_local'];
                    }
                    //获取当前版本商品支付方式
                    if (isset($isAstroDataFromLocal['goods_pay_type']))
                    {
                        $newData['goods_pay_type'] = $isAstroDataFromLocal['goods_pay_type'];
                    }
                    //获取当前版本准币支付方式
                    if (isset($isAstroDataFromLocal['coin_pay_type']))
                    {
                        $newData['coin_pay_type'] = $isAstroDataFromLocal['coin_pay_type'];
                    }
                    //获取当前版本苹果审核状态
                    if (isset($isAstroDataFromLocal['is_apple_pay']))
                    {
                        $newData['is_apple_pay'] = (int)$isAstroDataFromLocal['is_apple_pay'];
                    }
                    //获取当前版本苹果商品开关
                    if (isset($isAstroDataFromLocal['apple_goods']))
                    {
                        $newData['apple_goods'] = $isAstroDataFromLocal['apple_goods'];
                    }
                    //获取当前版本app LOGO
                    if (isset($isAstroDataFromLocal['logo_type']))
                    {
                        $newData['logo_type'] = (int)$isAstroDataFromLocal['logo_type'];
                    }
                    //是否开启广告栏
                    if (isset($isAstroDataFromLocal['is_open_ad']))
                    {
                        $newData['is_open_ad'] = (int)$isAstroDataFromLocal['is_open_ad'];
                    }
                    //获取当前安卓审核状态
                    if (isset($isAstroDataFromLocal['android_audit_mode_swtich']))
                    {
                        $newData['android_audit_mode_swtich'] = (int)$isAstroDataFromLocal['android_audit_mode_swtich'];
                    }
                }
            }
        }
        return $newData;
    }

    /**
     * 获取指定版本信息
     * @param $type
     * @param $version
     * @param array $field
     * @return array|bool|mixed
     * @throws \Exception
     */
    public function getVersionInfo($type, $version, $field = [])
    {
        $key = $type . ':' . $version;

        $versionName = 'V' . $version;

        $versionInfo = CacheManage::getInstance()->setCacheConfig($this->cacheModel, $key)->getDataAndSet(function () use ($type, $versionName)
        {
            return $this->getVersionDataByDb($type, $versionName, 0);
        }, $field);

        return $versionInfo;
    }

    /**
     * 获取版本信息
     * @param string $type 客户端类型  ios  android
     * @param string $version 版本号：1.9.4
     * @param int $status 特殊情况下，需要获取未开启的版本
     * @return array
     */
    protected function getVersionDataByDb($type, $version = null, int $status = 1): array
    {
        try
        {
            $where = ['type' => $type];
            if ($version)
            {
                $where['version_name'] = $version;
            }
            if ($status === 1)
            {
                $where['status'] = $status;
            }
            $data = $this
                ->field('id,version,version_name,android_audit_mode_swtich,update_content,force,is_astro_data_from_local,app_update,version_force,version_force_data,is_apple_pay,goods_pay_type,coin_pay_type,logo_type,apple_goods,is_open_ad,extend')
                ->where($where)
                ->order('id DESC')->findOrFail()->toArray();
        }
        catch (Exception $e)
        {
            return [];
        }

        return $data;
    }
}
